using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Framework
{
    public class GeraDAO
    {
        public static string Gerar(Type classe)
        {
            string txt = System.IO.File.ReadAllText("DAO.txt");
            
            PropertyInfo[] properties = classe.GetProperties();
            int numeroCampos = properties.Length;

            //Define o parametro #nome
            string nome = properties[0].Name.Replace("Id","");
            string lower = nome.ToLower();
            string table = "CREATE TABLE [dbo]["+nome+"]( ";

            //Monta o #insDom
            string insDom = "";
            string edit = "";
            string insVal = "";
            string parametros = "";
            string bind = lower + "." + properties[0].Name + " = Convert.ToInt32(reader[\"" + properties[0].Name + "\"].ToString()); \r\n";
            table += "["+properties[0].Name+"] [int] IDENTITY(1,1) NOT NULL ";
            for (int i = 1; i < numeroCampos; i++)
            {
                string tipo = properties[i].PropertyType.Name;

                if (tipo.Equals("String"))
                {
                    bind += lower + "." + properties[i].Name + " = reader[\"" + properties[i].Name + "\"].ToString(); \r\n";
                    table += ",[" + properties[i].Name + "] [varchar] (MAX)  NOT NULL ";
                }
                else
                {
                    bind += lower + "." + properties[i].Name + " = Convert.To"+tipo+"(reader[\"" + properties[i].Name + "\"].ToString()); \r\n";
                    table += ",[" + properties[i].Name + "] ["+ tipo +"] NOT NULL ";
                }

                if (i == 1 )
                {
                    insDom += properties[i].Name;
                    insVal += "@p" + i;
                    edit += properties[i].Name + " = @p" + i;
                    parametros += "query.Parameters.Add(new SqlParameter(\"p" + i + "\", obj." + properties[i].Name + ")); \r\n";
                }
                else
                {
                    insDom += "," + properties[i].Name;
                    insVal += ",@p" + i;
                    edit += "," + properties[i].Name + " = @p" + i;
                    parametros += "query.Parameters.Add(new SqlParameter(\"p" + i + "\", obj." + properties[i].Name + ")); \r\n";
                }
            }

            table += ")";

            txt = txt.Replace("#pack", "space");
            txt = txt.Replace("#nome", nome);
            txt = txt.Replace("#insDom", insDom);
            txt = txt.Replace("#insVal", insVal);
            txt = txt.Replace("#parametros", parametros);
            txt = txt.Replace("#edit", edit);
            txt = txt.Replace("#bind", bind);
            txt = txt.Replace("#lower", lower);

            System.IO.StreamWriter repository = new System.IO.StreamWriter(Environment.CurrentDirectory + "\\resultado.cs");
            repository.WriteLine(txt);

            repository.Close();

            System.IO.StreamWriter sql = new System.IO.StreamWriter(Environment.CurrentDirectory + "\\resultado.sql");
            table = table.Replace("Int32","int");
            sql.WriteLine(table);

            sql.Close();

            return "";
        }
    }
}
